<template>
  <el-card shadow="hover">
    <el-form inline>
      <el-form-item>
        <el-input placeholder="请输入用户名" v-model="userName"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" :icon="Search" @click="getUserList"
          >查询</el-button
        >
        <el-button @click="reset">清空</el-button>
      </el-form-item>
    </el-form>

    <el-form-item class="mt-20">
      <el-button
        type="primary"
        @click="showAddUser"
        v-if="userInfoStore.buttons.includes('btn.User.add')"
        >添 加</el-button
      >
      <el-button
        type="danger"
        disabled
        v-if="userInfoStore.buttons.includes('btn.User.remove')"
        >批量删除</el-button
      >
    </el-form-item>

    <el-table :data="userList" border class="mt-20" v-loading="loading">
      <el-table-column type="selection" width="60" align="center" />
      <el-table-column type="index" width="60" align="center" label="序号" />
      <el-table-column label="用户名" prop="username" width="200" />
      <el-table-column label="用户昵称" prop="nickName" width="200" />
      <el-table-column label="角色列表" prop="roleName" width="200" />
      <el-table-column label="创建时间" prop="gmtCreate" width="200" />
      <el-table-column label="更新时间" prop="gmtModified" width="200" />
      <el-table-column label="操作" width="200" fixed="right">
        <template v-slot="{ row }">
          <el-button
            type="info"
            :icon="User"
            size="small"
            @click="showAssignRole(row)"
            v-if="userInfoStore.buttons.includes('btn.User.assgin')" />

          <el-button
            type="warning"
            :icon="Edit"
            size="small"
            @click="showUpdateUser(row)"
            v-if="userInfoStore.buttons.includes('btn.User.update')" />

          <el-popconfirm
            :title="`确认删除 ${row.username} 吗?`"
            @confirm="delAttrValue(row.id)"
            width="200">
            <template #reference>
              <el-button
                type="danger"
                :icon="Delete"
                size="small"
                v-if="userInfoStore.buttons.includes('btn.User.remove')" />
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      class="mt-20"
      v-model:current-page="currentPage"
      v-model:page-size="pageSize"
      :total="total"
      :page-sizes="[5, 10, 15, 20]"
      layout="prev, pager, next, jumper, ->, sizes, total"
      @current-change="getUserList"
      @size-change="getUserList">
    </el-pagination>

    <el-dialog
      :title="`${addOrUpdateUserFormData.id ? '更新' : '添加'}用户`"
      v-model="addOrUpdateUserVisible">
      <el-form
        label-width="auto"
        ref="addOrUpdateUserFormRef"
        :model="addOrUpdateUserFormData"
        :rules="addOrUpdateUserFormRules">
        <el-form-item label="用户名" prop="username">
          <el-input
            placeholder="用户名"
            v-model="addOrUpdateUserFormData.username"></el-input>
        </el-form-item>
        <el-form-item label="用户名称" prop="nickName">
          <el-input
            placeholder="用户名称"
            v-model="addOrUpdateUserFormData.nickName"></el-input>
        </el-form-item>
        <el-form-item label="密 码" prop="password">
          <el-input
            placeholder="密 码"
            v-model="addOrUpdateUserFormData.password"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button type="primary" @click="addOrUpdateUser">保存</el-button>
        <el-button @click="addOrUpdateUserVisible = false">取消</el-button>
      </template>
    </el-dialog>

    <el-dialog title="分配用户角色" v-model="assignRoleVisible">
      <el-form
        label-width="auto"
        ref="assignRoleFormRef"
        :model="assignRoleFormData"
        :rules="assignRoleFormRules">
        <el-form-item label="用户名">
          <el-input
            placeholder="用户名"
            disabled
            v-model="assignRoleFormData.username"></el-input>
        </el-form-item>
        <el-form-item label="角色列表" prop="roleId">
          <el-checkbox
            v-model="checkAll"
            :indeterminate="isIndeterminate"
            @change="handleCheckAllChange">
            全选
          </el-checkbox>
          <el-checkbox-group
            v-model="assignRoleFormData.roleId"
            @change="handleCheckedRolesChange">
            <el-checkbox
              v-for="role in allRolesList"
              :key="role.id"
              :label="role.id">
              {{ role.roleName }}
            </el-checkbox>
          </el-checkbox-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button type="primary" @click="assignRole">保存</el-button>
        <el-button @click="assignRoleVisible = false">取消</el-button>
      </template>
    </el-dialog>
  </el-card>
</template>

<script lang="ts">
export default {
  name: "JUser",
};
</script>

<script lang="ts" setup>
import { Search, User, Edit, Delete } from "@element-plus/icons-vue";
import { ElMessage } from "element-plus";

import { ref, onMounted, reactive } from "vue";

import {
  getUserListApi,
  addUserApi,
  updateUserApi,
  removeUserApi,
  getUserRoleListApi,
  assignUserRoleListApi,
} from "@/api/acl/user";
import type {
  UserList,
  UserItem,
  AddOrUpdateUserFormData,
  RolesList,
} from "@/api/acl/model/userModel";
import type { CheckboxValueType } from "element-plus";

import { useUserInfoStore } from "@/stores/userInfo";
const userInfoStore = useUserInfoStore();

/********************* 分配用户角色列表***********************/
const assignRoleVisible = ref(false);
const checkAll = ref(false); //全选&全不选
const isIndeterminate = ref(false); // 半选
const allRolesList = ref<RolesList>([]);

const handleCheckAllChange = (val: CheckboxValueType) => {
  if (val) {
    // 全选
    assignRoleFormData.roleId = allRolesList.value.map((item) => item.id);
  } else {
    assignRoleFormData.roleId = [];
  }
  isIndeterminate.value = false;
};

const handleCheckedRolesChange = () => {
  if (assignRoleFormData.roleId.length === 0) {
    // 都未选
    isIndeterminate.value = false;
    checkAll.value = false;
  } else if (assignRoleFormData.roleId.length === allRolesList.value.length) {
    // 全选
    isIndeterminate.value = false;
    checkAll.value = true;
  } else {
    // 有一个选中
    isIndeterminate.value = true;
  }
};

const showAssignRole = async (row: UserItem) => {
  assignRoleVisible.value = true;
  assignRoleFormData.userId = row.id;
  assignRoleFormData.username = row.username;
  assignRoleFormData.roleId = [];
  assignRoleFormRef.value?.clearValidate();

  const res = await getUserRoleListApi(row.id);
  allRolesList.value = res.allRolesList;
  assignRoleFormData.roleId = res.assignRoles.map((item) => item.id);
  handleCheckedRolesChange();
};

const assignRoleFormRef = ref();
const assignRoleFormData = reactive<{
  userId: string;
  roleId: string[];
  username: string;
}>({
  userId: "",
  roleId: [],
  username: "",
});
const assignRoleFormRules = reactive({
  roleId: [{ required: true, message: "请选择至少一个角色" }],
});

const assignRole = async () => {
  await assignRoleFormRef.value.validate();
  await assignUserRoleListApi(
    assignRoleFormData.userId,
    assignRoleFormData.roleId
  );
  ElMessage.success("分配角色成功");
  getUserList();
  assignRoleVisible.value = false;
};
/********************* 删除用户***********************/
const delAttrValue = async (id: string) => {
  await removeUserApi(id);
  ElMessage.success("删除用户成功");
  getUserList();
};
/********************* 修改用户***********************/
const showUpdateUser = (row: UserItem) => {
  addOrUpdateUserVisible.value = true;
  addOrUpdateUserFormData.id = row.id;
  addOrUpdateUserFormData.username = row.username;
  addOrUpdateUserFormData.nickName = row.nickName;
  addOrUpdateUserFormData.password = "";
  addOrUpdateUserFormRef.value?.clearValidate();
};

/*********************添加用户***********************/
const addOrUpdateUserVisible = ref(false);
const addOrUpdateUserFormRef = ref();
const addOrUpdateUserFormData = reactive<AddOrUpdateUserFormData>({
  id: undefined,
  username: "",
  nickName: "",
  password: "",
});
const addOrUpdateUserFormRules = reactive({
  username: [{ required: true, message: "请输入用户名", trigger: "blur" }],
  nickName: [{ required: true, message: "请输入用户名称", trigger: "blur" }],
  password: [{ required: true, message: "请输入用户密码", trigger: "blur" }],
});

const showAddUser = () => {
  addOrUpdateUserVisible.value = true;
  addOrUpdateUserFormData.username = "";
  addOrUpdateUserFormData.nickName = "";
  addOrUpdateUserFormData.password = "";
  addOrUpdateUserFormRef.value?.clearValidate();
};

const addOrUpdateUser = async () => {
  await addOrUpdateUserFormRef.value.validate();
  const id = addOrUpdateUserFormData.id;
  if (id) {
    await updateUserApi({
      ...addOrUpdateUserFormData,
      id,
    });
  } else {
    await addUserApi(addOrUpdateUserFormData);
  }
  getUserList();
  addOrUpdateUserVisible.value = false;
  ElMessage.success(`${id ? "修改" : "添加"}用户成功`);
};

/*********************用户列表展示***********************/
const loading = ref(false);
const currentPage = ref(1);
const pageSize = ref(5);
const total = ref(0);
const userName = ref("");
const userList = ref<UserList>([]);

const getUserList = async () => {
  loading.value = true;
  const res = await getUserListApi({
    page: currentPage.value,
    limit: pageSize.value,
    username: userName.value,
  });
  total.value = res.total;
  userList.value = res.items;
  loading.value = false;
};

// 清空
const reset = () => {
  userName.value = "";
  getUserList();
};

onMounted(getUserList);
</script>

<style scoped></style>
